package com.cx.controller.back.api;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cx.entity.User;
import com.cx.service.IUserService;
import com.cx.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author cx
 * @since 2024-06-07
 */
@CrossOrigin    //解决跨域问题注解
@RestController
@RequestMapping("/back-user")
public class BackUserController {

    @Autowired
    private IUserService userService;

    @RequestMapping("login")
    public R loginById(@RequestBody Map<String, Object> person, HttpSession session) {
//            判断用户登录管理员的rid是否等于1
        person.put("rid", 1);
        session.setAttribute("userHome", person.get("username"));
        return userService.userLogin(person);
    }


    //    查询全部用户信息(分页条件查询)注意：lay-ui成功code为0
    @RequestMapping("list")
    public R getUserAll(@RequestParam Map<String, Object> params) {
        IPage<User> userIPage = userService.getUserListPageConditions(params);
        return R.ok(0, "用户数据请求成功").put("count", userIPage.getTotal()).put("data", userIPage.getRecords());
    }
    //    用户添加
    @RequestMapping("save")
    public R userSave(User user) {
        return R.ok().put("data", userService.save(user));
    }
    //    用户修改
    @RequestMapping("update")
    public R userUpdateById(User user) {
        return R.ok().put("data", userService.updateById(user));
    }

    //    用户删除
    @RequestMapping("del")
    public R delete(@RequestBody Integer[] ids) {//[1,2]
        userService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }
}
